package Work0609;

import java.awt.List;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 1:服务器上存有多个用户对象（包括用户和密码），客户端通过socket登录，如果成功，输出欢迎信息，失败给出提示。
 * 
 * @author Y
 *
 */
public class Socketservice extends Thread {

	private Socket a;
	static ArrayList<user> l = new ArrayList<user>();

	static {
		l.add(new user("heizhu", "qw123456"));
		l.add(new user("baizhu", "w123456"));
		l.add(new user("lanzhu", "q123456"));
	}

	public Socketservice(Socket a) {

		this.a = a;
		super.setName(String.valueOf(a.getInetAddress()));
	}

	public void run() {
		System.out.println(a.getInetAddress() + ":" + a.getPort() + "连接上了...");
		String str1;
		String str2;
		try {
			boolean q = true;
			InputStream in = a.getInputStream();
			OutputStream out = a.getOutputStream();
			BufferedReader i = new BufferedReader(new InputStreamReader(in));
			PrintWriter o = new PrintWriter(out);
			while (q) {
				str1 = i.readLine();
				str2 = i.readLine();

				for (int p = 0; p < l.size(); p++) {
					if (l.get(p).getName().equals(str1)) {
						if (l.get(p).getPass().equals(str2)) {
							o.println("登录成功");
							o.flush();
							q = false;
						}
					}
				}
				if (q == true) {
					o.println("登录失败");
					o.flush();
				}
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				a.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	}
}
